/**
 * 给mock请求统一加延迟.
 */
const delay = function (proxy, timer) {
    var mockApi:any = {};
    Object.keys(proxy).forEach(function (key) {
        var result = proxy[key].$body || proxy[key];
        if (Object.prototype.toString.call(result) === '[object String]' && /^http/.test(result)) {
            mockApi[key] = proxy[key];
        } else {
            mockApi[key] = function (req, res) {
                var foo;
                if (Object.prototype.toString.call(result) === '[object Function]') {
                    foo = result;
                } else {
                    foo = function (req, res) {
                        res.json(result);
                    };
                }

                setTimeout(function () {
                    foo(req, res);
                }, timer);
            };
        }
    });
    mockApi.__mockData = proxy;
    return mockApi;
};

export { delay };
